package com.zaoniao.enstudy.domain.service;

import com.zaoniao.enstudy.domain.entity.Vocabulary;
import com.zaoniao.enstudy.domain.entity.VocabularyWords;
import com.zaoniao.enstudy.domain.entity.bo.VocabularyWordsBo;
import com.zaoniao.enstudy.domain.repository.UserVocabularyRepository;
import com.zaoniao.enstudy.domain.repository.UserVocabularyWordsRepository;
import com.zaoniao.enstudy.infracore.constants.AppConstant;
import com.zaoniao.enstudy.infracore.exception.AppException;
import com.zaoniao.enstudy.infracore.web.CommonError;
import com.zaoniao.enstudy.infracore.web.PagedGridResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VocabularyWordsService {

    private final UserVocabularyWordsRepository vocabularyWordsRepository;
    private final UserVocabularyRepository vocabularyRepository;

    /**
     * 收藏单词.
     *
     * @param vocabularyWords 待收藏的单词信息.
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addVocabularyWords(VocabularyWords vocabularyWords) {
        // 检查词汇表是否为空，为空创建一个"我想要记住"的词汇表
        boolean isHaveVocabularies = vocabularyRepository.checkCountByUserId(vocabularyWords.getUserId());
        if (!isHaveVocabularies && vocabularyWords.getVocabularyId() == null) {
            // 新增词汇表"我想要记住"
            Vocabulary vocabulary = new Vocabulary();
            vocabulary.setUserId(vocabularyWords.getUserId());
            vocabulary.setVocabularyName(AppConstant.DEFAULT_VOCABULARY_NAME);
            Vocabulary newVocabulary = vocabularyRepository.insert(vocabulary);
            vocabularyWords.setVocabularyId(newVocabulary.getId());
        }
        if(vocabularyWords.getVocabularyId() == null){
            throw new AppException(CommonError.BusinessError, "词汇表Id不能为空");
        }
        // 检查单词是否已存在待新增的词汇表,不存在，新增单词收藏
        boolean isHaveVocabularyWords = vocabularyWordsRepository.checkVocabularyWords(vocabularyWords);
        if (!isHaveVocabularyWords) {
            vocabularyWordsRepository.insert(vocabularyWords);
        } else {
            throw new AppException(CommonError.BusinessError, "单词已存在该词汇表");
        }
    }

    /**
     * 更换词汇表.
     *
     * @param vocabularyWords 待更换的单词信息.
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updVocabularyWords(VocabularyWords vocabularyWords) {
        vocabularyWordsRepository.updVocabularyWords(vocabularyWords);
    }

    /**
     * 取消收藏.
     *
     * @param vocabularyWordsId 单词明细Id.
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updStatus(Integer vocabularyWordsId) {
        vocabularyWordsRepository.updStatus(vocabularyWordsId);
    }

    /**
     * 根据词汇表Id查询词汇表所有收藏的单词
     *
     * @param vocabularyId 词汇表Id
     * @param page         第几页
     * @param size         每页显示数量
     * @return 单词列表分页数据
     */
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public PagedGridResult<VocabularyWordsBo> getVocabularyWordsPage(Integer vocabularyId, Integer page, Integer size) {
        return vocabularyWordsRepository.getVocabularyWordsPage(vocabularyId, page, size);
    }

}
